import { isString, isEmpty } from "@pureadmin/utils";
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
import {
  useRouter,
  useRoute,
  type LocationQueryRaw,
  type RouteParamsRaw
} from "vue-router";

export function useDetail() {
  const route = useRoute();
  const router = useRouter();
  const getParameter = isEmpty(route.params) ? route.query : route.params;

  function toDetail(
    parameter: LocationQueryRaw | RouteParamsRaw,
    model:
      | "examine"
      | "salecontract"
      | "purchasecontract"
      | "purchaseapply"
      | "supplyapply"
      | "reconciliation"
      | "reconciliationSettlement"
      | "spreconciliation"
      | "spreconciliationSettlement"
      | "receive"
      | "payment"
      | "ElevatorOrder"
      | "PurchaseContractAdvancePayment"
      | "SalesContractAdvancePayment"
      | "ProjectOrderAdvancePayment"
      | "SupplyOrderAdvancePayment"
  ) {
    // ⚠️ 这里要特别注意下，因为vue-router在解析路由参数的时候会自动转化成字符串类型，比如在使用useRoute().route.query或useRoute().route.params时，得到的参数都是字符串类型
    // 所以在传参的时候，如果参数是数字类型，就需要在此处 toString() 一下，保证传参跟路由参数类型一致都是字符串，这是必不可少的环节！！！
    Object.keys(parameter).forEach(param => {
      if (!isString(parameter[param])) {
        parameter[param] = parameter[param].toString();
      }
    });
    // 审批
    if (model === "examine") {
      // 保存信息到标签页
      useMultiTagsStoreHook().handleTags("push", {
        path: "/welcome/components/examine",
        name: "examine",
        query: parameter,
        meta: {
          title: `【审核】${parameter.title}`
        }
      });
      // 路由跳转
      router.push({ name: "examine", query: parameter });
    }
    // 查看销售合同
    else if (model === "salecontract") {
      // 保存信息到标签页
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_sales/components/salesContractDetail",
        name: "salesContractDetail",
        query: parameter,
        meta: {
          title: `【查看】“${parameter.title}”销售合同`
        }
      });
      // 路由跳转
      router.push({ name: "salesContractDetail", query: parameter });
    }
    // 查看采购合同
    else if (model === "purchasecontract") {
      // 保存信息到标签页
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_procure/components/procureContractDetail",
        name: "procureContractDetail",
        query: parameter,
        meta: {
          title: `【查看】“${parameter.title}”采购合同`
        }
      });
      // 路由跳转
      router.push({ name: "procureContractDetail", query: parameter });
    }
    // 查看项目订单
    else if (model === "purchaseapply") {
      // 保存信息到标签页
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_OrderManage/ERP_ProjectOrder/components/addProjectOrder",
        name: "addProjectOrder",
        query: parameter,
        meta: {
          title: `【查看】“${parameter.title}”项目订单`
        }
      });
      // 路由跳转
      router.push({ name: "addProjectOrder", query: parameter });
    }
    // 查看供货订单
    else if (model === "supplyapply") {
      // 保存信息到标签页
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_SupplyManage/ERP_SupplygoodsOrder/components/SelectSupplygoodsOrder",
        name: "SelectSupplygoodsOrder",
        query: parameter,
        meta: {
          title: `【查看】“${parameter.title}”供货订单`
        }
      });
      router.push({ name: "SelectSupplygoodsOrder", query: parameter });
    }
    // 查看项目对账单
    else if (model === "reconciliation") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/project/components/projectReconciliationDetail",
        name: "projectReconciliationDetail",
        query: parameter,
        meta: {
          title: `【查看】“${parameter.title}”项目对账单`
        }
      });
      router.push({ name: "projectReconciliationDetail", query: parameter });
    }
    // 查看项目结算单
    else if (model === "reconciliationSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/project/components/projectSettlementDetail",
        name: "projectSettlementDetail",
        query: parameter,
        meta: {
          title: `【查看】“${parameter.title}”项目结算单`
        }
      });
      router.push({ name: "projectSettlementDetail", query: parameter });
    }
    // 查看供应商对账单
    else if (model === "spreconciliation") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/supplier/components/supplierReconciliationDetail",
        name: "supplierReconciliationDetail",
        query: parameter,
        meta: {
          title: `【查看】“${parameter.title}”供应商对账单`
        }
      });
      router.push({ name: "supplierReconciliationDetail", query: parameter });
    }
    // 查看供应商结算单
    else if (model === "spreconciliationSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/supplier/components/supplierSettlementDetail",
        name: "supplierSettlementDetail",
        query: parameter,
        meta: {
          title: `【查看】“${parameter.title}”供应商结算单`
        }
      });
      router.push({ name: "supplierSettlementDetail", query: parameter });
    }
    // 查看收款管理
    else if (model === "receive") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_PaymentMange/ERP_CollectionPaymentMange/components/addCollectionPaymentMange",
        name: "addCollectionPaymentMange",
        query: parameter,
        meta: {
          title: `【查看】“${parameter.title}”收款管理`
        }
      });
      router.push({ name: "addCollectionPaymentMange", query: parameter });
    }
    // 查看付款管理
    else if (model === "payment") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_PaymentMange/ERP_PaymentMange/components/addPaymentMange",
        name: "addPaymentMange",
        query: parameter,
        meta: {
          title: `【查看】“${parameter.title}”付款管理`
        }
      });
      router.push({ name: "addPaymentMange", query: parameter });
    }
    // 查看项目发票
    // 查看供应商发票
    // 查看电梯订单
    else if (model === "ElevatorOrder") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_PaymentMange/ERP_PaymentMange/components/addElevatorOrder",
        name: "ElevatorOrder",
        query: parameter,
        meta: {
          title: `【查看】“${parameter.title}”电梯订单`
        }
      });
      router.push({ name: "ElevatorOrder", query: parameter });
    }
    // 查看采购合同预付款
    else if (model === "PurchaseContractAdvancePayment") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_PaymentMange/ERP_CollectInAdvance/ERP_PurchaseContractAdvancePayment/components/viewPurchaseContractAdvancePayment",
        name: "PurchaseContractAdvancePayment",
        query: parameter,
        meta: {
          title: `【查看】“${parameter.title}”采购合同预付款`
        }
      });
      router.push({ name: "PurchaseContractAdvancePayment", query: parameter });
    }
    // 查看销售合同预收款
    else if (model === "SalesContractAdvancePayment") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_PaymentMange/ERP_PaymentInAdvance/ERP_SalesContractAdvancePayment/components/viewSalesContractAdvancePayment",
        name: "SalesContractAdvancePayment",
        query: parameter,
        meta: {
          title: `【查看】“${parameter.title}销售合同预收款`
        }
      });
      router.push({ name: "SalesContractAdvancePayment", query: parameter });
    }
    // 查看项目订单预收款
    else if (model === "ProjectOrderAdvancePayment") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_PaymentMange/ERP_PaymentInAdvance/ERP_ProjectOrderAdvancePayment/components/viewProjectOrderAdvancePayment",
        name: "ProjectOrderAdvancePayment",
        query: parameter,
        meta: {
          title: `【查看】“${parameter.title}”项目订单预收款`
        }
      });
      router.push({ name: "ProjectOrderAdvancePayment", query: parameter });
    }
    // 查看供货订单预付款
    else if (model === "SupplyOrderAdvancePayment") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_PaymentMange/ERP_CollectInAdvance/ERP_SupplyOrderAdvancePayment/components/viewSupplyOrderAdvancePayment",
        name: "SupplyOrderAdvancePayment",
        query: parameter,
        meta: {
          title: `【查看】“${parameter.title}”供货订单预收款`
        }
      });
      router.push({ name: "SupplyOrderAdvancePayment", query: parameter });
    }
  }

  // 用于页面刷新，重新获取浏览器地址栏参数并保存到标签页
  const initToDetail = (
    model:
      | "examine"
      | "salecontract"
      | "purchasecontract"
      | "purchaseapply"
      | "supplyapply"
      | "reconciliation"
      | "reconciliationSettlement"
      | "spreconciliation"
      | "spreconciliationSettlement"
      | "receive"
      | "payment"
      | "ElevatorOrder"
      | "PurchaseContractAdvancePayment"
      | "SalesContractAdvancePayment"
      | "ProjectOrderAdvancePayment"
      | "SupplyOrderAdvancePayment"
  ) => {
    if (getParameter) toDetail(getParameter, model);
  };

  return { toDetail, initToDetail, getParameter, router };
}
